# -*- coding: utf-8 -*-
########################################################################
#   Pax-Selector. Copyright 2010, Hector Sanjuán.                      #
#                                                                      #
#   Pax-selector is free software: you can redistribute it and/or      #
#   modify it under the terms of the GNU General Public License as     #
#   published by the Free Software Foundation, either version 3 of     #
#   the License, or any later version.                                 #
#                                                                      #
#   Pax-selector is distributed in the hope that it will be useful,    #
#   but WITHOUT ANY WARRANTY; without even the implied warranty of     #
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the      #
#   GNU General Public License for more details.                       #
#                                                                      #
#   You should have received a copy of the GNU General Public License. #
#   If not, see <http://www.gnu.org/licenses/>.                        #
#                                                                      #
########################################################################

require 'paxUtils'

class UserInfo
  attr_reader :user, :group, :id
  
  def self.authenticate user,password
    if PaxUtils.users[user] == password
      return self.new(user)
    end
    nil
  end

  def self.get_from_id id
    self.new(PaxUtils.usernames[id])
  end

  def initialize(username)
    @user = username
    @id = PaxUtils.usernames.index(username)
    @userfile = "users/#{username}.user"
    @group = {}
    begin
      file = File.new(@userfile, "r")
    rescue
      file = File.new(@userfile, "w")
      file.puts "## Voting file for #{@user}"
    else
      file.readline #skip comment
      file.each do |line|
        #["id", "name", "mark","comment"]
        array = line.chomp.split('#')
        id = array[0]
        # TODO separate module to parse info from file?
        info = {
          :name => array[1],
          :mark => array[2], 
          :comment => array[3]
        }
        
        @group[id.to_i] = info if array[2] or array[3]
      end
      
    ensure
      file.close unless file.nil?
    end
  end
  
  
  def name id
    #id => {:name,:mark}
    indiv = @group[id]
    indiv[:name] if indiv
  end

  
  def mark id
    #id => {:name,:mark}
    indiv = @group[id]
    mark = indiv[:mark] if indiv
    return mark unless mark==""
  end

  def prettyMark id 
    indiv = @group[id]
    mark = indiv[:mark] if indiv
    return nil if mark==""
    case mark
      when "1" then return "bad"
      when "2" then return "medium"
      when "3" then return "good"
      else return mark
    end

  end

  def size
    @group.size
  end

  def comment id
    indiv = @group[id]
    indiv[:comment] if indiv
  end

  def has? id
    @group[id]
  end
  
  def save
    file = File.new(@userfile, "w")
    file.puts "## Voting file for #{@user}"
    @group.each do |id,info|
      file.puts "#{id}##{info[:name]}##{info[:mark]}##{info[:comment]}"
    end
    file.close
  end

  def update (id,name,mark,comment,save=nil)
    indiv = @group[id]
    needs_update = (!indiv or indiv[:mark] != mark or indiv[:comment]!=comment)
    if needs_update
      @group[id] = {:name => name, :mark => mark, :comment => comment}
      save() if save
    end
    return 2 if needs_update and save #updated and saved
    return 1 if needs_update #updated
    return 0
  end
  
end
